package com.fowo.api.service.handleWork;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fowo.api.datainterface.util.k3cloud.domain.Authentication;
import com.fowo.api.datainterface.util.k3cloud.domain.BillSave;
import com.fowo.api.entity.ReceivableProduct;
import com.fowo.api.model.receivable.ReceivablePushJinideAction;
import com.fowo.api.model.receivable.ReceivableVo;
import com.fowo.api.service.handleWork.jindieEntity.jindieResult;
import com.fowo.api.service.handleWork.jindieEntity.jindieVoucher;
import com.fowo.api.service.impl.ReceivableProductServiceImpl;
import com.fowo.api.service.impl.ReceivableServiceImpl;
import com.fowo.api.util.LocalDateUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class receivablePushJinide implements ReceivablePushJinideAction {
    @Resource
    private ReceivableServiceImpl receivableService;
    @Resource
    private ReceivableProductServiceImpl receivableProductService;
    private static final Authentication auth=new Authentication("64e8964fb4af0d","Administrator","a@123456");

    private static final K3CloudTemplate k3loudTemplate=new K3CloudTemplate("http://121.225.97.82:8001/K3cloud/",auth);
    @Override
    public void run(Long id) throws Exception {
        ReceivableVo voById = receivableService.getVoById(id);
        Assert.notNull(voById,"不存在!");
        Assert.isTrue(voById.getStatus().equals("1"),"单据未审核不能推送!");
        Assert.isTrue(voById.getPushStatus().equals("0"),"已推送!");
        List<ReceivableProduct> list1 = receivableProductService.list(Wrappers.<ReceivableProduct>query().lambda().eq(ReceivableProduct::getParentId, id));
        BigDecimal reduce = list1.stream().map(ReceivableProduct::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);

        BillSave billSave = new BillSave("GL_VOUCHER");
        jindieVoucher jindieVoucher=new  jindieVoucher();
        jindieVoucher.setIsAutoAdjustField("true");
        jindieVoucher.ModelDTO  modelDTO=new jindieVoucher.ModelDTO();
        modelDTO.setFvoucherid(0);
        //账簿
        jindieVoucher.ModelDTO.FAccountBookIDDTO FAccountBookIDDTO=new  jindieVoucher.ModelDTO.FAccountBookIDDTO();
        FAccountBookIDDTO.setFNumber("002");
        modelDTO.setFAccountBookID(FAccountBookIDDTO);
        modelDTO.setFDate(LocalDateUtils.format(voById.getBusinessDate(),LocalDateUtils.DATE_PATTERN));//日期
        //凭证字
        jindieVoucher.ModelDTO.FVOUCHERGROUPIDDTO FVOUCHERGROUPIDDTO=new jindieVoucher.ModelDTO.FVOUCHERGROUPIDDTO();
        FVOUCHERGROUPIDDTO.setFNumber("PRE001");
        modelDTO.setFvouchergroupid(FVOUCHERGROUPIDDTO);
        modelDTO.setFvouchergroupno("1");
        modelDTO.setFDocumentStatus("Z");
        List<jindieVoucher.ModelDTO.FEntityDTO> list=new ArrayList<>();
        jindieVoucher.ModelDTO.FEntityDTO entity = createEntity("1122.02",reduce.doubleValue(),null,"",voById.getPayCode());
        //01001
        jindieVoucher.ModelDTO.FEntityDTO entity1 = createEntity("6051",null,reduce.doubleValue(),null,voById.getPayCode());
        list.add(entity);
        list.add(entity1);
        modelDTO.setFEntity(list);
        jindieVoucher.setModel(modelDTO);
        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(jindieVoucher, false, false);
        billSave.setModel(stringObjectMap);
        String result = k3loudTemplate.executeBillSave(billSave, String.class);
        jindieResult jindieResult = JSONObject.parseObject(result, jindieResult.class);
        if(jindieResult.getResult().getResponseStatus().getIsSuccess().equals("true")){
            voById.setPushStatus("1");
            voById.setResultInfo("推送完成");
        }else{
            voById.setPushStatus("2");
            voById.setResultInfo(jindieResult.getResult().getResponseStatus().getErrors().toString());
        }
        receivableService.update(voById);
    }


    private static jindieVoucher.ModelDTO.FEntityDTO createEntity(String kemu,Double jiefang,Double daifang,String gongchang,String code){
        jindieVoucher.ModelDTO.FEntityDTO fEntityDTO=new jindieVoucher.ModelDTO.FEntityDTO();
        //科目编码
        jindieVoucher.ModelDTO.FEntityDTO.FACCOUNTIDDTO FACCOUNTIDDTO=new jindieVoucher.ModelDTO.FEntityDTO.FACCOUNTIDDTO();
        FACCOUNTIDDTO.setFNumber(kemu);
        fEntityDTO.setFaccountid(FACCOUNTIDDTO);
        fEntityDTO.setFexplanation("出品原材料收入,单号:"+code);
        //币别
        jindieVoucher.ModelDTO.FEntityDTO.FCURRENCYIDDTO  FCURRENCYIDDTO=new jindieVoucher.ModelDTO.FEntityDTO.FCURRENCYIDDTO();
        FCURRENCYIDDTO.setFNumber("PRE001");
        fEntityDTO.setFcurrencyid(FCURRENCYIDDTO);
        //汇率
        jindieVoucher.ModelDTO.FEntityDTO.FEXCHANGERATETYPEDTO FEXCHANGERATETYPEDTO=new jindieVoucher.ModelDTO.FEntityDTO.FEXCHANGERATETYPEDTO();
        FEXCHANGERATETYPEDTO.setFNumber("HLTX01_SYS");
        fEntityDTO.setFexchangeratetype(FEXCHANGERATETYPEDTO);
        //借方金额
        fEntityDTO.setFdebit(jiefang);//
        fEntityDTO.setFamountfor(jiefang);//原币金额
        // 贷方金额
        fEntityDTO.setFcredit(daifang);
        //设置供应商
        if(gongchang!=null){
            jindieVoucher.ModelDTO.FEntityDTO.FDetailIDDTO FDetailIDDTO=new jindieVoucher.ModelDTO.FEntityDTO.FDetailIDDTO();
            jindieVoucher.ModelDTO.FEntityDTO.FDetailIDDTO.FDETAILIDFFLEX6DTO  FDETAILIDFFLEX4DTO=new  jindieVoucher.ModelDTO.FEntityDTO.FDetailIDDTO.FDETAILIDFFLEX6DTO();
            FDETAILIDFFLEX4DTO.setFNumber("0013");
            FDetailIDDTO.setFDETAILID__FFLEX6(FDETAILIDFFLEX4DTO);
            fEntityDTO.setFDetailID(FDetailIDDTO);
        }
        return fEntityDTO;
    }


}
